package studio.raptor.ddal.core.log.bo;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by liujy on 2017/10/16.
 */
public class SqlProcessLogData implements Serializable,Cloneable{

    private static final long serialVersionUID = 25183447344323L;

    private String traceId;
    private String startTime;
    private String endTime;
    private long processTime;
    private String sqlType;
    private Collection<String> shards;
    private String hostIp;
    private HashSet<String> tables;
    private String sql;
    private List<Object> parameters;
    private String plan;
    private List<String> dbUsers;
    private List<String> dataSources;
    private Boolean isSucess=true;
    private String errorMsg="";
    //采集时间
    private Date acceptTime;
    //不同分片最终执行sql不一样
    private List<String> finalSqlList;

    public SqlProcessLogData() {
        dbUsers=new ArrayList<>( );
        dataSources=new ArrayList<>( );
        parameters=new ArrayList<>( );
        finalSqlList=new ArrayList<>(  );
    }

    //跟踪ID
    public SqlProcessLogData(String traceId, Date startTime, Date endTime, long processTime, String sqlType, Collection<String> shards, String hostIp, Set<String> tables, String sql, List<Object> parameters, String plan, List<String> dbUsers, List<String> dataSources) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        this.traceId = traceId;
        this.startTime = formatter.format(startTime);
        this.endTime = formatter.format(endTime);
        this.processTime = processTime;
        this.sqlType = sqlType;
        this.shards = shards;
        this.hostIp = hostIp;
        this.tables = new HashSet(tables);
        this.sql = sql;
        this.parameters = parameters;
        this.plan = plan;
        this.dbUsers = dbUsers;
        this.dataSources = dataSources;
    }

    @Override
    public String toString() {
        return "{" +
                "\"traceId\":\"" + traceId + '\"' +
                ",\"startTime\":\"" + startTime +'\"'+
                ",\"endTime\":\"" + endTime  +'\"'+
                ",\"processTime\":\"" + processTime + "ms" +'\"'+
                ",\"sqlType\":\"" + sqlType +'\"'+
                ",\"shards\":\"" + shards +'\"'+
                ",\"hostIp\":\"" + hostIp +'\"'+
                ",\"tables\":\"" + tables +'\"'+
                ",\"sql\":\"" + sql +'\"'+
                ",\"parameters\":\"" + parameters +'\"'+
                ",\"plan\":\"" + plan +'\"'+
                ",\"dataSource\":\"" + dataSources +'\"'+
                ",\"dbUser\":\"" + dbUsers +'\"'+
                ",\"isSucess\":\"" + getSucessStr() +'\"'+
                ",\"errorMsg\":\"" + errorMsg +'\"'+
                '}';
    }

    public Object clone(){
        try{
            return super.clone();
        }catch (Exception e){
            return null;
        }
    }

    public String getTraceId() {
        return traceId;
    }

    public void setTraceId(String traceId) {
        this.traceId = traceId;
    }

    public String getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        this.startTime = formatter.format(startTime);
    }

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        this.endTime = formatter.format(endTime);
    }

    public long getProcessTime() {
        return processTime;
    }

    public void setProcessTime(long processTime) {
        this.processTime = processTime;
    }

    public String getSqlType() {
        return sqlType;
    }

    public void setSqlType(String sqlType) {
        this.sqlType = sqlType;
    }

    public Collection<String> getShards() {
        return shards;
    }

    public void setShards(Collection<String> shards) {
        this.shards = shards;
    }

    public String getHostIp() {
        return hostIp;
    }

    public void setHostIp(String hostIp) {
        this.hostIp = hostIp;
    }

    public Set<String> getTables() {
        return tables;
    }

    public void setTables(Set<String> tables) {
        this.tables = new HashSet(tables);
    }

    public String getSql() {
        return sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }

    public List<Object> getParameters() {
        return parameters;
    }

    /**
     * StatementContext.sqlParameters 为FastArrayList类型，转化为ArrayList类型
     * @param parameters
     */
    public void setParameters(List<Object> parameters) {
        for(int i=0;i<parameters.size();i++){
            this.parameters.add( parameters.get( i ) );
        }
//        this.parameters = parameters;
    }

    public String getPlan() {
        return plan;
    }

    public void setPlan(String plan) {
        this.plan = plan;
    }

    public List<String> getDbUsers() {
        return dbUsers;
    }

    public void setDbUsers(List<String> dbUsers) {
        this.dbUsers = dbUsers;
    }

    public List<String> getDataSources() {
        return dataSources;
    }

    public void setDataSources(List<String> dataSources) {
        this.dataSources = dataSources;
    }

    public String getSucessStr() {
        return isSucess ? "Y":"N";
    }

    public Boolean getSucess() {
        return isSucess;
    }

    public void setSucess(Boolean sucess) {
        isSucess = sucess;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public Date getAcceptTime() {
        return acceptTime;
    }

    public void setAcceptTime(Date acceptTime) {
        this.acceptTime = acceptTime;
    }

    public List<String> getFinalSqlList() {
        return finalSqlList;
    }

    public void setFinalSqlList(List<String> finalSqlList) {
        this.finalSqlList = finalSqlList;
    }
}
